﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapQPZUORD(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveQPZUORD(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryQPZUORD(dwData, 4);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapQPZUORD(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertQPZUORD(dwLocal);
            }
            UpdateQPZUORD(dwLocal,ITXDBVERSION);
        }
        public void InsertQPZUORD(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            //APZMANDANT APZART APZVAR APZANRKRS APZALFDNR APZSTUFE
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO QPZUORD(QPZMANDANT, QPZARTST, QPZKUNDNR, QPZWERK, QPZABT, QPZART, QPZVAR, QPZPNRKRS, QPZPLFDNR, QPZAENZL, QPZAENB, QPZAEND, QPZNEUB, QPZNEUD, QPZSPEZIF, QPZSPSTA, QPZSPGRD, QPZZUSTAND, QPZPKUNDNR, QPZPADRSL, QPZLIEFNR, QPZLAFDL, QPZLADS, QPZFKZ, QPZAUFBER, QPZDRTRB, QPZFILLER)"
                                + " VALUES (:QPZMANDANT,:QPZARTST, '00000000', '   ', '   ',:QPZART, :QPZVAR, :QPZPNRKRS,:QPZPLFDNR, 0, '        ', 0, :QPZNEUB, :QPZNEUD,:QPZSPEZIF, 'N', '                              ', '    ', '        ', '        ', '        ', '        ', '        ', '  ', '  ', '     ', '            ')";
            or1.Parameters.Add("QPZMANDANT", OracleType.NVarChar).Value = dwData["QPZMANDANT"].ToString();
            or1.Parameters.Add("QPZARTST", OracleType.NVarChar).Value = dwData["QPZARTST"].ToString();
            or1.Parameters.Add("QPZART", OracleType.NVarChar).Value = dwData["QPZART"].ToString();
            or1.Parameters.Add("QPZVAR", OracleType.NVarChar).Value = dwData["QPZVAR"].ToString();
            or1.Parameters.Add("QPZPNRKRS", OracleType.NVarChar).Value = dwData["QPZPNRKRS"].ToString();
            or1.Parameters.Add("QPZPLFDNR", OracleType.NVarChar).Value = dwData["QPZPLFDNR"].ToString();
            or1.Parameters.Add("QPZNEUB", OracleType.NVarChar).Value = dwData["QPZNEUB"].ToString();
            or1.Parameters.Add("QPZNEUD", OracleType.NVarChar).Value = dwData["QPZNEUD"].ToString();
            or1.Parameters.Add("QPZSPEZIF", OracleType.NVarChar).Value = dwData["QPZSPEZIF"].ToString();
            setResult = Connect2Proxy(or1);
        }
        public void DeleteQPZUORD(string client, string stage, string art, string var)
        {
            OracleCommand or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE QPZUORD "
                        + " WHERE QPZMANDANT=:QPZMANDANT  AND QPZARTST=:QPZARTST AND QPZART=:QPZART  AND QPZVAR=:QPZVAR";
            or1.Parameters.Add("QPZMANDANT", OracleType.NVarChar).Value = client;
            or1.Parameters.Add("QPZARTST", OracleType.NVarChar).Value = stage;
            or1.Parameters.Add("QPZART", OracleType.NVarChar).Value = art;
            or1.Parameters.Add("QPZVAR", OracleType.NVarChar).Value = var;
            Connect2Proxy(or1);
        }
        public DataSet QueryQPZUORD(DataRow dwData, int option = 1)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();

            OracleCommand or1;
           
                or1 = OracleDB.CreateCommand();
                switch (option)
                {
                    case 1:
                        or1.CommandText = "SELECT* FROM QPZUORD"
                            + " WHERE  QPZARTST='F' AND QPZART=:QPZART AND QPZVAR=:QPZVAR";
                        or1.Parameters.Add("QPZART", OracleType.NVarChar).Value = dwData["QPZART"].ToString();
                        or1.Parameters.Add("QPZVAR", OracleType.NVarChar).Value = dwData["QPZVAR"].ToString();
                        break;
                    case 2:
                        or1.CommandText = "SELECT* FROM QPZUORD"
                            + " WHERE QPZMANDANT=:QPZMANDANT AND QPZARTST='F' AND QPZPNRKRS=:QPZPNRKRS AND QPZPLFDNR=:QPZPLFDNR";
                        or1.Parameters.Add("QPZMANDANT", OracleType.NVarChar).Value = dwData["QPZMANDANT"].ToString();
                        or1.Parameters.Add("QPZPNRKRS", OracleType.NVarChar).Value = dwData["QPZPNRKRS"].ToString();
                        or1.Parameters.Add("QPZPLFDNR", OracleType.NVarChar).Value = dwData["QPZPLFDNR"].ToString();
                        break;
                    case 3:
                        or1.CommandText = "SELECT* FROM QPZUORD"
                            + " WHERE QPZMANDANT=:QPZMANDANT AND QPZARTST='F' AND QPZART=:QPZART AND QPZVAR=:QPZVAR AND QPZPNRKRS=:QPZPNRKRS AND QPZPLFDNR=:QPZPLFDNR";
                        or1.Parameters.Add("QPZMANDANT", OracleType.NVarChar).Value = dwData["QPZMANDANT"].ToString();
                        or1.Parameters.Add("QPZART", OracleType.NVarChar).Value = dwData["QPZART"].ToString();
                        or1.Parameters.Add("QPZVAR", OracleType.NVarChar).Value = dwData["QPZVAR"].ToString();
                        or1.Parameters.Add("QPZPNRKRS", OracleType.NVarChar).Value = dwData["QPZPNRKRS"].ToString();
                        or1.Parameters.Add("QPZPLFDNR", OracleType.NVarChar).Value = dwData["QPZPLFDNR"].ToString();
                        break;
                    case 4:
                        or1.CommandText = "SELECT* FROM QPZUORD"
                            + " WHERE QPZMANDANT=:QPZMANDANT AND QPZARTST='F' AND QPZART=:QPZART AND QPZVAR=:QPZVAR ";
                        or1.Parameters.Add("QPZMANDANT", OracleType.NVarChar).Value = dwData["QPZMANDANT"].ToString();
                        or1.Parameters.Add("QPZART", OracleType.NVarChar).Value = dwData["QPZART"].ToString();
                        or1.Parameters.Add("QPZVAR", OracleType.NVarChar).Value = dwData["QPZVAR"].ToString();
                        break;
                    default:
                        or1.CommandText = "SELECT* FROM QPZUORD"
                            + " WHERE  QPZARTST='F' AND QPZART=:QPZART AND QPZVAR=:QPZVAR";
                        or1.Parameters.Add("QPZART", OracleType.NVarChar).Value = dwData["QPZART"].ToString();
                        or1.Parameters.Add("QPZVAR", OracleType.NVarChar).Value = dwData["QPZVAR"].ToString();
                        break;
                }

                SResult = Connect2Proxy(or1);
                if (SResult.Tables.Count > 0)
                {
                    SResult.Tables[0].TableName = "QPZUORD";
                    SReturn.Merge(SResult);
                } 
            return SReturn;
        }
        public void UpdateQPZUORD(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch(ITXDBVERSION)
            {
                case "JLT":
                    or1.CommandText = "UPDATE QPZUORD SET "
               + "QPZMANDANT=:QPZMANDANT,"
               + "QPZARTST=:QPZARTST,"
               + "QPZKUNDNR=:QPZKUNDNR,"
               + "QPZWERK=:QPZWERK,"
               + "QPZABT=:QPZABT,"
               + "QPZART=:QPZART,"
               + "QPZVAR=:QPZVAR,"
               + "QPZPNRKRS=:QPZPNRKRS,"
               + "QPZPLFDNR=:QPZPLFDNR,"
               + "QPZAENZL=:QPZAENZL,"
               + "QPZAENB=:QPZAENB,"
               + "QPZAEND=:QPZAEND,"
               + "QPZNEUB=:QPZNEUB,"
               + "QPZNEUD=:QPZNEUD,"
               + "QPZSPEZIF=:QPZSPEZIF,"
               + "QPZSPSTA=:QPZSPSTA,"
               + "QPZSPGRD=:QPZSPGRD,"
               + "QPZZUSTAND=:QPZZUSTAND,"
               + "QPZPKUNDNR=:QPZPKUNDNR,"
               + "QPZPADRSL=:QPZPADRSL,"
               + "QPZLIEFNR=:QPZLIEFNR,"
               + "QPZLAFDL=:QPZLAFDL,"
               + "QPZLADS=:QPZLADS,"
               + "QPZFKZ=:QPZFKZ,"
               + "QPZAUFBER=:QPZAUFBER,"
               + "QPZDRTRB=:QPZDRTRB,"
               + "QPZFILLER=:QPZFILLER"
               + " WHERE  QPZMANDANT=:QPZMANDANT AND QPZART=:QPZART AND QPZVAR=:QPZVAR ";
                    break;
                default:
            or1.CommandText = "UPDATE QPZUORD SET "
                + "QPZMANDANT=:QPZMANDANT,"
                + "QPZARTST=:QPZARTST,"
                + "QPZKUNDNR=:QPZKUNDNR,"
                + "QPZWERK=:QPZWERK,"
                + "QPZABT=:QPZABT,"
                + "QPZART=:QPZART,"
                + "QPZVAR=:QPZVAR,"
                + "QPZPNRKRS=:QPZPNRKRS,"
                + "QPZPLFDNR=:QPZPLFDNR,"
                + "QPZAENZL=:QPZAENZL,"
                + "QPZAENB=:QPZAENB,"
                + "QPZAEND=:QPZAEND,"
                + "QPZNEUB=:QPZNEUB,"
                + "QPZNEUD=:QPZNEUD,"
                + "QPZSPEZIF=:QPZSPEZIF,"
                + "QPZSPSTA=:QPZSPSTA,"
                + "QPZSPGRD=:QPZSPGRD,"
                + "QPZZUSTAND=:QPZZUSTAND,"
                + "QPZPKUNDNR=:QPZPKUNDNR,"
                + "QPZPADRSL=:QPZPADRSL,"
                + "QPZLIEFNR=:QPZLIEFNR,"
                + "QPZLAFDL=:QPZLAFDL,"
                + "QPZLADS=:QPZLADS,"
                + "QPZFKZ=:QPZFKZ,"
                + "QPZAUFBER=:QPZAUFBER,"
                + "QPZDRTRB=:QPZDRTRB,"
                + "QPZFILLER=:QPZFILLER"
                + " WHERE  QPZMANDANT=:QPZMANDANT AND QPZART=:QPZART AND QPZVAR=:QPZVAR AND QPZPNRKRS=:QPZPNRKRS AND QPZPLFDNR=:QPZPLFDNR ";
            break;
        }
            or1.Parameters.Add("QPZMANDANT", OracleType.NVarChar).Value = dwData["QPZMANDANT"].ToString();
            or1.Parameters.Add("QPZARTST", OracleType.NVarChar).Value = dwData["QPZARTST"].ToString();
            or1.Parameters.Add("QPZKUNDNR", OracleType.NVarChar).Value = dwData["QPZKUNDNR"].ToString();
            or1.Parameters.Add("QPZWERK", OracleType.NVarChar).Value = dwData["QPZWERK"].ToString();
            or1.Parameters.Add("QPZABT", OracleType.NVarChar).Value = dwData["QPZABT"].ToString();
            or1.Parameters.Add("QPZART", OracleType.NVarChar).Value = dwData["QPZART"].ToString();
            or1.Parameters.Add("QPZVAR", OracleType.NVarChar).Value = dwData["QPZVAR"].ToString();
            or1.Parameters.Add("QPZPNRKRS", OracleType.NVarChar).Value = dwData["QPZPNRKRS"].ToString();
            or1.Parameters.Add("QPZPLFDNR", OracleType.NVarChar).Value = dwData["QPZPLFDNR"].ToString();
            or1.Parameters.Add("QPZAENZL", OracleType.NVarChar).Value = dwData["QPZAENZL"].ToString();
            or1.Parameters.Add("QPZAENB", OracleType.NVarChar).Value = dwData["QPZAENB"].ToString();
            or1.Parameters.Add("QPZAEND", OracleType.NVarChar).Value = dwData["QPZAEND"].ToString();
            or1.Parameters.Add("QPZNEUB", OracleType.NVarChar).Value = dwData["QPZNEUB"].ToString();
            or1.Parameters.Add("QPZNEUD", OracleType.NVarChar).Value = dwData["QPZNEUD"].ToString();
            or1.Parameters.Add("QPZSPEZIF", OracleType.NVarChar).Value = dwData["QPZSPEZIF"].ToString();
            or1.Parameters.Add("QPZSPSTA", OracleType.NVarChar).Value = dwData["QPZSPSTA"].ToString();
            or1.Parameters.Add("QPZSPGRD", OracleType.NVarChar).Value = dwData["QPZSPGRD"].ToString();
            or1.Parameters.Add("QPZZUSTAND", OracleType.NVarChar).Value = dwData["QPZZUSTAND"].ToString();
            or1.Parameters.Add("QPZPKUNDNR", OracleType.NVarChar).Value = dwData["QPZPKUNDNR"].ToString();
            or1.Parameters.Add("QPZPADRSL", OracleType.NVarChar).Value = dwData["QPZPADRSL"].ToString();
            or1.Parameters.Add("QPZLIEFNR", OracleType.NVarChar).Value = dwData["QPZLIEFNR"].ToString();
            or1.Parameters.Add("QPZLAFDL", OracleType.NVarChar).Value = dwData["QPZLAFDL"].ToString();
            or1.Parameters.Add("QPZLADS", OracleType.NVarChar).Value = dwData["QPZLADS"].ToString();
            or1.Parameters.Add("QPZFKZ", OracleType.NVarChar).Value = dwData["QPZFKZ"].ToString();
            or1.Parameters.Add("QPZAUFBER", OracleType.NVarChar).Value = dwData["QPZAUFBER"].ToString();
            or1.Parameters.Add("QPZDRTRB", OracleType.NVarChar).Value = dwData["QPZDRTRB"].ToString();
            or1.Parameters.Add("QPZFILLER", OracleType.NVarChar).Value = dwData["QPZFILLER"].ToString();
            Connect2Proxy(or1);
        }
    }
}
